<?php
/**
 * Created by PhpStorm.
 * User: Windows
 * Date: 2015/11/17
 * Time: 11:51
 */

class InquireModel extends BaseModel
{
    private $_table;
    protected $_isbug = false;//�Ƿ����õ���SQlģʽ
    public function __construct() {
        parent::__construct();
    }
    /**
     * @param string $where
     * @param string $limit
     * @return array
     */
    public function getNewInquire($where = "1=1",$limit="0,10"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from("iq_inquiry as iq",array("num","status","update_time"))
            ->joinLeft("users_invoice as ui","iq.business_id =ui.id ",array("business_name","contacts","mobile"))
            ->joinLeft("product as p","iq.product_id = p.id",array("product_name"))
            ->joinLeft("product_brands as pb","p.brand_id = pb.id",array("brand_name"))
            ->joinLeft("product_cates as pc","pc.id = p.cate_id",array("cate_name"))
            ->where($where)
            ->order("iq.id DESC")
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        if(!empty($limit)){
            $sql.=" limit {$limit}";
        }
        return $db->fetchAll ( $sql );
    }

    /**
     * @param $were
     * @return string
     */
    public function getInquireCountByWhere($were){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( "iq_inquiry","count(*)" )
            ->joinLeft ( "res_service","iq_inquiry.service_id = res_service.id","")
            ->joinLeft ( "res_currency","iq_inquiry.currency_id = res_currency.id","")
            ->joinLeft ( "product", "iq_inquiry.product_id = product.id", "")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->joinLeft ( "users_invoice", "iq_inquiry.business_id = users_invoice.id","")
            ->joinLeft("region","region.region_id = iq_inquiry.area_id","")
            ->where ( $were )
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchOne( $sql );
    }

    /**
     * @param $where
     * @param $item_page
     * @param $offset
     * @param $order
     * @param $group
     * @return array
     */
    public function getInquireListByWhere($where,$item_page,$offset,$order,$group){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( "iq_inquiry" )
            ->joinLeft ( "res_service","iq_inquiry.service_id = res_service.id",array("service_name"))
            ->joinLeft ( "res_currency","iq_inquiry.currency_id = res_currency.id",array("mark as currency_mark","name as currency_name"))
            ->joinLeft ( "product", "iq_inquiry.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice", "iq_inquiry.business_id = users_invoice.id",array ( "business_name","business_phone","contacts","mobile","qq"))
            ->joinLeft("region","region.region_id = iq_inquiry.area_id","region_name as area_name")
            ->where ( $where )
            ->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }
        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        $data = $db->fetchAll ( $sql );
        if($data){
            foreach($data as $k=>$v){
                $data[$k]['update_time'] = Tools::time2Units(time() - strtotime($v['update_time'])) . "ǰ";
            }
        }
        return $data;
    }

    /**
     * @param $id
     * @return int
     */
    public function delinquire($id){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        return $db->delete("iq_inquiry","id in ({$id})");
    }

    public function getInquireList($where = "1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from("iq_inquiry as iq")
            ->joinLeft("users_invoice as ui","iq.business_id =ui.id ",array("business_name","contacts","mobile"))
            ->joinLeft("product as p","iq.product_id = p.id",array("product_name"))
            ->joinLeft("product_brands as pb","p.brand_id = pb.id",array("brand_name"))
            ->joinLeft("product_cates as pc","pc.id = p.cate_id",array("cate_name"))
            ->joinLeft ( "res_service","iq.service_id = res_service.id",array("service_name"))
            ->joinLeft ( "res_currency","iq.currency_id = res_currency.id",array("mark as currency_mark","name as currency_name"))
            ->where($where)
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }

        return $db->fetchRow ( $sql );
    }

}